package com.dxx.cloud.admin.utils;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dxx.cloud.admin.module.entity.BaseEntity;
import org.dromara.common.core.domain.R;

import java.util.List;

public class BaseControllerUtils {

    /**
     * 添加表中记录
     * @throws Exception
     */
    public static <T extends BaseEntity> R add(BaseMapper<T> mapper, Class<T> clazz, Object request){
        final Long l = EntityUtils.addEntity(mapper, clazz, request);
        return EntityUtils.buildResponseModelByInteger(l);
    }

    /**
     * 根据id更新表中记录
     */
    public static <T extends BaseEntity> R updateById(BaseMapper<T> mapper, Long id, Object request) {
        final Long l = EntityUtils.updateEntityById(mapper, id, request);
        return EntityUtils.buildResponseModelByInteger(l);
    }

    public static <T extends BaseEntity> R updateByIdWithoutUpdated(BaseMapper<T> mapper, Long id, Object request) throws Exception{
        final Long l = EntityUtils.updateEntityByIdWithoutUpdated(mapper, id, request);
        return EntityUtils.buildResponseModelByInteger(l);
    }

    public static <T extends BaseEntity,RES> R<List<RES>> list(BaseMapper<T> mapper, Class<T> entityClazz, Class<RES> responseClazz, Object request) throws Exception{
        final List<RES> res = EntityUtils.responseList(mapper, entityClazz, responseClazz, request);
        return R.ok(res);
    }
}
